package core;
import java.io.File;
import java.io.IOException;

import repository.Repository;
import stage.Index;

public class JitAdd {

    /**
     * add file into stage
     * @param file
     * @throws Exception
     */
    public static void add(File file) throws Exception {
        if(!file.exists()) {  //file
            throw new IOException("File doesn't exist.");
        }

        Index index = Index.getInstance();
        if(file.isFile()){
            if(index.getFileIndex(file) > -1) {  //if the file already exists in stage, overwrite it
                index.removeIndex(file);
            }
            index.insertIndex(file);
        }

        else if(file.isDirectory() && !file.getName().equals(".jit")){  //directory
            File[] fs = file.listFiles();
            for(int i = 0; i < fs.length; i++){
                add(fs[i]);  //recursion
            }
        }
    }
}

